package gen.tools.jqGrid;

import gen.tools.Widgets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryEmpsInJqGrid extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryEmpsInJqGrid() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String strSQL = "SELECT COUNT(empId) FROM emps";
		
		ResultTableSet resultTableSet = new ResultTableSet();
		int sysSN = Integer.parseInt( request.getParameter("sysSN") );
		String sord = request.getParameter("sord");
		String sidx =  request.getParameter("sidx");
		
		int page = 1;
		int total_pages = 1;
		int count = 0;
		int limit = 6;

		if ("".equalsIgnoreCase(sidx)|| sidx==null) {
			sidx = "t1.empId";
		} 
		if ("".equalsIgnoreCase(sord)|| sord==null) {
			sord = "ASC";
		} else {
			page = Integer.parseInt(request.getParameter("page"));
			limit = Integer.parseInt(request.getParameter("rows"));
		}
		
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			rs.next();
			count = rs.getInt(1);

			if (count != 0) {

				if (limit < count) {
					total_pages = (count / limit) + 1;
				}
				strSQL = "SELECT t1.empId,t1.empPhone,t1.empPwd,t1.empName,t1.empEmail FROM emps t1 WHERE sysSN=? ORDER BY "+sidx+" "+sord+" LIMIT "+(limit*page-limit)+","+ limit;
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, sysSN);
				rs = ps.executeQuery();
				ArrayList<Rows> rows = new ArrayList<Rows>();

				while (rs.next()) {
					String empId = rs.getString("empId");
					Rows row = new Rows();
					String[] cell = new String[5];
					
					row.setId(empId);
					cell[0] = empId;
					cell[1] = rs.getString("empName");
					cell[2] = rs.getString("empPhone");
					cell[3] = rs.getString("empEmail");
					cell[4] = rs.getString("empPwd");
					
					row.setCell(cell);
					rows.add(row);
				}
				
				resultTableSet.setRows(rows);
			} else {
				page = 0;
				total_pages = 0;
			}
			resultTableSet.setPage(String.valueOf(page));
			resultTableSet.setTotal(total_pages);
			resultTableSet.setRecords(String.valueOf(count));
			
		} catch ( Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		
		response.getWriter().write(new Gson().toJson(resultTableSet));	
	
	}

}
